package bootstrap

import (
	"gxcvu-ai/app/middlewares"
	"gxcvu-ai/pkg/response"
	"gxcvu-ai/routers"
	"strings"

	"github.com/gin-gonic/gin"
)

func SetupRouter(router *gin.Engine) {

	registerGlobalMiddlware(router)

	routers.RegisterApiRouters(router)

	Setup404Handler(router)
}

func registerGlobalMiddlware(router *gin.Engine) {
	router.Use(
		middlewares.Logger(),
		middlewares.Recovery(),
		middlewares.CORS(),
	)
}

func Setup404Handler(router *gin.Engine) {
	// 处理 404 请求
	router.NoRoute(func(c *gin.Context) {
		// 获取标头信息的 Accept 信息
		acceptString := c.Request.Header.Get("Accept")
		if strings.Contains(acceptString, "text/html") {
			// 如果是 HTML 的话
			response.Abort404(c)
		} else {
			// 默认返回 JSON
			response.Abort404(c)
		}
	})
}
